package com.registration.task;

import com.registration.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class PaymentTimeoutTask {

    @Autowired
    private PaymentService paymentService;

    @Scheduled(fixedRate = 60000) // 每1分钟检查一次
    public void cancelTimeoutPayments() {
        try {
            int timeoutMinutes = 15; // 超时时间设为2分钟,
            int count = paymentService.cancelTimeoutPendingPayments(timeoutMinutes);
            if (count > 0) {
                System.out.println("已取消 " + count + " 个超时未支付的订单");
            }
        } catch (Exception e) {
            System.err.println("取消超时订单任务执行失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}